package com.mew.slots.core.game.hdcz.room;

import com.mew.slots.core.common.game.BaseRoom;
import com.mew.slots.core.common.game.vo.BasePlayMsgs;
import com.mew.slots.core.game.hdcz.bo.HdczInRoom;
import com.mew.slots.core.game.hdcz.roller.Controlline;
import com.mew.slots.core.game.hdcz.roller.HdczIli;

import java.util.ArrayList;
import java.util.Arrays;

public class HdczRoom extends BaseRoom {

    final int _FREE = 8;
    HdczInRoom inRoom;
    int changeRate;
    //消除倍数
    final int[] change_multiples={1,2,3,5};

    public HdczRoom(HdczInRoom inRoom,int changeRate){
        this.inRoom=inRoom;
        this.changeRate=changeRate;
        res = new BasePlayMsgs();
        row = 3;
        col = 5;
        _WILD = 7;
        spes = new ArrayList<Integer>(){{
            add(_FREE);
        }};
        rate = new int[][]{
                {0, 0, 3, 10, 45},      //藏宝图
                {0, 0, 4, 15, 75},      //指南针
                {0, 0, 5, 20, 100},     //药水
                {0, 0, 10, 25, 200},    //手枪
                {0, 0, 15, 50, 500},    //大炮
                {0, 0, 20, 100, 1000},  //包袱
                {0, 0, 50, 250, 2500},  //骷髅头
                {0, 0, 0, 0, 0},        //百搭W
                {0, 0, 0, 0, 0}         //鹦鹉Scatter
        };
        alllines=new int[][]{
                {5,6,7,8,9},
                {0,1,2,3,4},
                {10,11,12,13,14},
                {0,6,12,8,4},
                {10,6,2,8,14},
                {0,1,7,3,4},
                {10,11,7,13,14},
                {5,11,12,13,9},
                {5,1,2,3,9},
                {0,6,7,8,4},
                {10,6,7,8,14},
                {5,6,2,8,9},
                {5,6,12,8,9},
                {5,1,7,3,9},
                {5,11,7,13,9},
                {0,6,2,8,4},
                {10,6,12,8,14},
                {0,1,7,13,14},
                {10,11,7,3,4},
                {0,11,2,13,4}
        };
    }

    @Override
    /**
     * 取线
     */
    public void getLinesXml(){
        if (controlType>0){
            endSlots = control();
        } else {
            endSlots = rollerXml.getDownLine(free?1:0,rollerID,inRoom.getIdx(),inRoom.getMove());
        }
        res.endSlots = endSlots.clone();
    }



    @Override
    public void checkaward(){

        int[] rl;
        int type;
        int num;

        for (int j = 0;j< iconLines.length;j++) {

            BasePlayMsgs.LineResult bgo = new BasePlayMsgs.LineResult();
            bgo.lineNum = j;
            rl = iconLines[j].clone();
            type = rl[0];
            num = 0;
            //不为特殊图标
            if (spes.contains(type)){
                continue;
            }

            //确定类型
            for(int i=0;i<rl.length;i++){
                if(rl[i]!=_WILD){
                    type=rl[i];
                    break;
                }
            }

            for (int i = 0; i < rl.length; i++) {
                if (type==rl[i]||rl[i] == _WILD) {
                    num++;
                }else {
                    break;
                }
            }
            Integer rate0 = rate[type][num - 1];
            if (rate0>0){
                bgo.mult = rate0;
                bgo.type = type;
                bgo.num = num;
                bgo.uvIdx = Arrays.stream(lines[j]).limit(num).toArray();
                res.lines.add(bgo);
            }

        }

        for(BasePlayMsgs.LineResult x:res.lines)
        {
            res.multiple += x.mult;
        }
        res.multiple*=change_multiples[changeRate]*(free?3:1);
    }
    @Override
    public void freeBouns(){
        int freeNum=fsdb(_FREE);
        if (freeNum>2) {
            res.setFree((byte)1);
            res.setFreeNumber(10);
        }
        if(res.getMultiple()>0){
            res.setChange(true);
            remove();
            inRoom.setMove(endSlots);
        }else{
            res.setChange(false);
            restoreMove();
        }
    }

    private void remove(){
        for (BasePlayMsgs.LineResult line : res.getLines()) {
            for (int uvIdx : line.getUvIdx()) {
                endSlots[uvIdx]=-1;
            }
        }
    }

    /**
     * 还原move
     */
    public void restoreMove(){
        for (int i=0;i<inRoom.getMove().length;i++) {
            inRoom.getMove()[i]=-1;
        }
        for (int i = 0; i < inRoom.getIdx().length; i++) {
            inRoom.getIdx()[i]=null;
        }
    }

    @Override
    public int[] control(){
        Controlline controlline=new Controlline();
        switch (controlType) {
            case 1:
                return bigwin();
            case 2:
                return controlline.free();
            default:
                return controlline.empty();
        }
    }
}
